package com.edais.util;

import org.apache.log4j.Logger;

public class IDCard {
	private static final Logger logger = Logger.getLogger(IDCard.class);
	
	//15位转18位身份证
	public static String uptoeighteen(String fifteencardid){
		String eightcardid = fifteencardid.substring(0, 6);
		eightcardid = eightcardid + "19";
		eightcardid = eightcardid + fifteencardid.substring(6, 15);
		eightcardid = eightcardid + getVerify(eightcardid);
		return eightcardid;	
	}
	
	//获得校验位
	public static String getVerify(String eightcardid){
		int[] ai = new int[18];
		int[] vi = {1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2};
		int[] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
		
		int remaining = 0;
		if (eightcardid.length() == 18) {
			eightcardid = eightcardid.substring(0, 17);
		}
		if (eightcardid.length() == 17) {
			int sum = 0;
			for (int i = 0; i < 17; i++) {
				String k = eightcardid.substring(i, i + 1);
				ai[i] = Integer.parseInt(k);
			}
			for (int i = 0; i < 17; i++) {
				sum = sum + wi[i] * ai[i];
			}
			remaining = sum % 11;
			}
		return remaining == 2 ? "X" : String.valueOf(vi[remaining]);
		}
	
	// 获取出生年份
	public static String getYear(String code){
		return code.substring(6,10);
	}
	// 获取出生月份
	public static String getMonth(String code){
		return code.substring(10,12);
	}
	// 获取出生日
	public static String getDay(String code){
		return code.substring(12,14);
	}
	// 获取性别
	public static String getSex(String code){
		String id17 = code.substring(16,17);
		if (Integer.parseInt(id17) % 2 != 0) {     
            return  "MALE";//男
        } else {     
           return "FEMALE";//女
        } 
	}
	
	// 获取生日
	public static String getBirthDay(String code){
		return code.substring(6,14);
	}
	
	public static void main(String[] args){
		IDCard i = new IDCard();
		String code = "350802198411092548";
		System.out.println(i.getSex(code));
		System.out.println(i.getDay(code));
		System.out.println(i.getMonth(code));
		System.out.println(i.getYear(code));
		System.out.println(i.getBirthDay(code));
	}
}
